package stardust.core.java.reflect;


import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.LinkedList;
import java.util.List;


public class AnnotationReflect
{

  public static <T extends Annotation> T getAnnotation(Method method, Class<T> ant)
  {
    return method.getAnnotation(ant);
  }

  public static Field[] getAnnotationField(Class c)
  {
    Field[] fields = FieldReflect.getFields(c);
    List<Field> fs = new LinkedList<Field>();

    for (int i = 0; i < fields.length; i++)
    {
      if (fields[i].getAnnotations().length > 0)
      {
        fs.add(fields[i]);
      }
    }

    return fs.toArray(new Field[0]);
  }

  public static Field[] getAnnotationField(Class c, Class a)
  {
    Field[] fields = FieldReflect.getFields(c);
    List<Field> fs = new LinkedList<Field>();

    for (int i = 0; i < fields.length; i++)
    {
      if (fields[i].getAnnotation(a) != null)
      {
        fs.add(fields[i]);
      }
    }

    return fs.toArray(new Field[0]);
  }

  public static <T extends Annotation> T getAnnotation(Class<?> cls, Class<T> ant)
  {
    T a;

    do
    {
      a = cls.getAnnotation(ant);

      if (a != null)
      {
        return a;
      }
    }
    while ((cls = cls.getSuperclass()) != java.lang.Object.class && cls != null);

    return null;
  }

}
